import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["systemNoticeHistorySelectedRowKeys", "systemNoticeHistorySelectedRows"];

/**
 * model 公告管理
 * @date: 2018-8-6


 */
import { createPagination, getCurrentOrganizationId, getResponse, parseParameters } from 'utils/utils';
import { queryFileList as _queryFileList, queryMapIdpValue, queryUUID, removeFileList } from 'cpaas-front/lib/services/api';
import { createNotice as _createNotice, createReceiver as _createReceiver, deleteNotice as _deleteNotice, fetchNotice as _fetchNotice, publicNotice as _publicNotice, publishSystemNotice as _publishSystemNotice, queryNotice as _queryNotice, queryNoticeType, queryReceiver as _queryReceiver, querySystemHistory as _querySystemHistory, removeDraftReceive as _removeDraftReceive, revokeNotice as _revokeNotice, updateNotice as _updateNotice, uploadImage as _uploadImage } from "../services/cmsgNoticeService";
export default {
  namespace: 'cmsgNotice',
  state: {
    noticeList: [],
    // 公告列表数据
    pagination: {},
    // 分页对象
    noticeDetail: {
      // 公告明细信息
      noticeContent: {
        noticeBody: ''
      }
    },
    noticeReceiverType: [],
    // 公告接受者类型
    noticeCategory: [],
    // 公告类别
    noticeStatus: [],
    // 公告状态
    noticeType: [],
    // 公告类型
    langList: [],
    // 语言列表
    noticeCascaderType: [],
    // 级联数据
    receiverRecordType: [],
    // 接收者类型
    systemNoticePublishModalVisible: false,
    // 发送系统消息公告
    systemNoticePublishRecord: {},
    // 发送当前系统消息公告的记录
    systemNoticeHistoryDataSource: [],
    // 历史信息 数据
    systemNoticeHistoryPagination: false,
    // 历史信息 分页
    systemNoticeHistorySelectedRows: [],
    // 历史信息 选中数据
    systemNoticeHistorySelectedRowKeys: [],
    // 历史信息 选中数据key
    systemNoticeReceiveDataSource: [],
    // 接收信息 数据
    systemNoticeReceivePagination: false // 接收信息 分页

  },
  effects: {
    // 获取初始化数据
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, langList, noticeType, noticeCascaderType, _getResponse, noticeReceiverType, noticeCategory, receiverRecordType, noticeStatus;

      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              langList = [];
              _context.t0 = getResponse;
              _context.next = 5;
              return call(queryNoticeType, {
                'CMSG.NOTICE.NOTICE_TYPE': 1,
                'CMSG.NOTICE.NOTICE_TYPE.CH': 2
              });

            case 5:
              _context.t1 = _context.sent;
              noticeType = (0, _context.t0)(_context.t1);
              _context.t2 = getResponse;
              _context.next = 10;
              return call(queryNoticeType, {
                'CMSG.NOTICE.RECERVER_TYPE': 1,
                'CMSG.NOTICE.NOTICE_CATEGORY': 2
              });

            case 10:
              _context.t3 = _context.sent;
              noticeCascaderType = (0, _context.t2)(_context.t3);
              _context.t4 = getResponse;
              _context.next = 15;
              return call(queryMapIdpValue, {
                noticeReceiverType: 'CMSG.NOTICE.RECERVER_TYPE',
                noticeCategory: 'CMSG.NOTICE.NOTICE_CATEGORY',
                receiverRecordType: 'CMSG.NOTICE.RECEIVER_RECORD_TYPE',
                noticeStatus: 'CMSG.NOTICE.STATUS'
              });

            case 15:
              _context.t5 = _context.sent;
              _getResponse = (0, _context.t4)(_context.t5);
              noticeReceiverType = _getResponse.noticeReceiverType;
              noticeCategory = _getResponse.noticeCategory;
              receiverRecordType = _getResponse.receiverRecordType;
              noticeStatus = _getResponse.noticeStatus;
              _context.next = 23;
              return put({
                type: 'updateState',
                payload: {
                  noticeReceiverType: noticeReceiverType,
                  noticeStatus: noticeStatus,
                  noticeType: noticeType,
                  noticeCascaderType: noticeCascaderType,
                  noticeCategory: noticeCategory,
                  langList: langList,
                  receiverRecordType: receiverRecordType // 接收者类型

                }
              });

            case 23:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    fetchNotice: /*#__PURE__*/_regeneratorRuntime.mark(function fetchNotice(_ref2, _ref3) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchNotice$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(_fetchNotice, parseParameters(payload));

            case 4:
              res = _context2.sent;
              list = getResponse(res);

              if (!list) {
                _context2.next = 9;
                break;
              }

              _context2.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  noticeList: list.content,
                  pagination: createPagination(list)
                }
              });

            case 9:
              return _context2.abrupt("return", list);

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchNotice);
    }),
    // 创建公告信息
    createNotice: /*#__PURE__*/_regeneratorRuntime.mark(function createNotice(_ref4, _ref5) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createNotice$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.next = 4;
              return call(_createNotice, payload);

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, createNotice);
    }),
    // 更新公告信息
    updateNotice: /*#__PURE__*/_regeneratorRuntime.mark(function updateNotice(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function updateNotice$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_updateNotice, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, updateNotice);
    }),
    // 删除公告信息
    deleteNotice: /*#__PURE__*/_regeneratorRuntime.mark(function deleteNotice(_ref8, _ref9) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deleteNotice$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_deleteNotice, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, deleteNotice);
    }),
    // 查询单条公告信息
    queryNotice: /*#__PURE__*/_regeneratorRuntime.mark(function queryNotice(_ref10, _ref11) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function queryNotice$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call, put = _ref11.put;
              _context6.next = 4;
              return call(_queryNotice, payload);

            case 4:
              res = _context6.sent;
              list = getResponse(res);

              if (!list) {
                _context6.next = 9;
                break;
              }

              _context6.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  noticeDetail: list
                }
              });

            case 9:
              return _context6.abrupt("return", list);

            case 10:
            case "end":
              return _context6.stop();
          }
        }
      }, queryNotice);
    }),
    // 发布公告信息
    publicNotice: /*#__PURE__*/_regeneratorRuntime.mark(function publicNotice(_ref12, _ref13) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function publicNotice$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call;
              _context7.next = 4;
              return call(_publicNotice, payload);

            case 4:
              res = _context7.sent;
              return _context7.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, publicNotice);
    }),
    // 撤销删除公告信息
    revokeNotice: /*#__PURE__*/_regeneratorRuntime.mark(function revokeNotice(_ref14, _ref15) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function revokeNotice$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call;
              _context8.next = 4;
              return call(_revokeNotice, payload);

            case 4:
              res = _context8.sent;
              return _context8.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context8.stop();
          }
        }
      }, revokeNotice);
    }),
    // 获取文件
    queryFileList: /*#__PURE__*/_regeneratorRuntime.mark(function queryFileList(_ref16, _ref17) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function queryFileList$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref16.payload;
              call = _ref17.call;
              _context9.next = 4;
              return call(_queryFileList, payload);

            case 4:
              res = _context9.sent;
              return _context9.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, queryFileList);
    }),
    // 查询UUID
    fetchUuid: /*#__PURE__*/_regeneratorRuntime.mark(function fetchUuid(_, _ref18) {
      var call, organizationId, res;
      return _regeneratorRuntime.wrap(function fetchUuid$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              call = _ref18.call;
              organizationId = getCurrentOrganizationId();
              _context10.next = 4;
              return call(queryUUID, {
                tenantId: organizationId
              });

            case 4:
              res = _context10.sent;
              return _context10.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, fetchUuid);
    }),
    // 删除文件
    removeFile: /*#__PURE__*/_regeneratorRuntime.mark(function removeFile(_ref19, _ref20) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function removeFile$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              payload = _ref19.payload;
              call = _ref20.call;
              _context11.next = 4;
              return call(removeFileList, payload);

            case 4:
              res = _context11.sent;
              return _context11.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context11.stop();
          }
        }
      }, removeFile);
    }),
    // 富文本上传图片
    uploadImage: /*#__PURE__*/_regeneratorRuntime.mark(function uploadImage(_ref21, _ref22) {
      var payload, file, call, res;
      return _regeneratorRuntime.wrap(function uploadImage$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              payload = _ref21.payload, file = _ref21.file;
              call = _ref22.call;
              _context12.next = 4;
              return call(_uploadImage, payload, file);

            case 4:
              res = _context12.sent;
              return _context12.abrupt("return", res);

            case 6:
            case "end":
              return _context12.stop();
          }
        }
      }, uploadImage);
    }),
    // 删除草稿中的 接收配置
    removeDraftReceive: /*#__PURE__*/_regeneratorRuntime.mark(function removeDraftReceive(_ref23, _ref24) {
      var payload, call, record, res;
      return _regeneratorRuntime.wrap(function removeDraftReceive$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              payload = _ref23.payload;
              call = _ref24.call;
              record = payload.record;
              _context13.next = 5;
              return call(_removeDraftReceive, record);

            case 5:
              res = _context13.sent;
              return _context13.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context13.stop();
          }
        }
      }, removeDraftReceive);
    }),
    // 查询历史发布信息
    querySystemHistory: /*#__PURE__*/_regeneratorRuntime.mark(function querySystemHistory(_ref25, _ref26) {
      var payload, call, put, noticeId, query, res, parseRes;
      return _regeneratorRuntime.wrap(function querySystemHistory$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              payload = _ref25.payload;
              call = _ref26.call, put = _ref26.put;
              noticeId = payload.noticeId, query = payload.query;
              _context14.next = 5;
              return call(_querySystemHistory, noticeId, query);

            case 5:
              res = _context14.sent;
              parseRes = getResponse(res);

              if (!parseRes) {
                _context14.next = 10;
                break;
              }

              _context14.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  systemNoticeHistoryDataSource: parseRes.content,
                  systemNoticeHistoryPagination: createPagination(parseRes),
                  systemNoticeHistorySelectedRows: [],
                  systemNoticeHistorySelectedRowKeys: [],
                  systemNoticeReceiveDataSource: [],
                  systemNoticeReceivePagination: false
                }
              });

            case 10:
            case "end":
              return _context14.stop();
          }
        }
      }, querySystemHistory);
    }),
    // 查询 发布信息 对应的接收配置
    queryReceiver: /*#__PURE__*/_regeneratorRuntime.mark(function queryReceiver(_ref27, _ref28) {
      var payload, call, put, noticeId, query, _payload$prevSystemNo, prevSystemNoticeHistorySelectedRowKeys, _query$systemNoticeHi, systemNoticeHistorySelectedRowKeys, systemNoticeHistorySelectedRows, relQuery, res, parseRes, partialState;

      return _regeneratorRuntime.wrap(function queryReceiver$(_context15) {
        while (1) {
          switch (_context15.prev = _context15.next) {
            case 0:
              payload = _ref27.payload;
              call = _ref28.call, put = _ref28.put;
              noticeId = payload.noticeId, query = payload.query, _payload$prevSystemNo = payload.prevSystemNoticeHistorySelectedRowKeys, prevSystemNoticeHistorySelectedRowKeys = _payload$prevSystemNo === void 0 ? [] : _payload$prevSystemNo;
              _query$systemNoticeHi = query.systemNoticeHistorySelectedRowKeys, systemNoticeHistorySelectedRowKeys = _query$systemNoticeHi === void 0 ? prevSystemNoticeHistorySelectedRowKeys : _query$systemNoticeHi, systemNoticeHistorySelectedRows = query.systemNoticeHistorySelectedRows, relQuery = _objectWithoutProperties(query, _excluded);

              if (!((systemNoticeHistorySelectedRowKeys || []).length !== 0)) {
                _context15.next = 16;
                break;
              }

              _context15.next = 7;
              return call(_queryReceiver, noticeId, relQuery, systemNoticeHistorySelectedRowKeys);

            case 7:
              res = _context15.sent;
              parseRes = getResponse(res);
              partialState = {
                systemNoticeReceiveDataSource: parseRes.content,
                systemNoticeReceivePagination: createPagination(parseRes)
              };

              if (systemNoticeHistorySelectedRows) {
                partialState.systemNoticeHistorySelectedRows = systemNoticeHistorySelectedRows;
                partialState.systemNoticeHistorySelectedRowKeys = systemNoticeHistorySelectedRowKeys;
              }

              if (!parseRes) {
                _context15.next = 14;
                break;
              }

              _context15.next = 14;
              return put({
                type: 'updateState',
                payload: partialState
              });

            case 14:
              _context15.next = 18;
              break;

            case 16:
              _context15.next = 18;
              return put({
                type: 'updateState',
                payload: {
                  systemNoticeReceiveDataSource: [],
                  // 接收信息 数据
                  systemNoticeReceivePagination: false,
                  // 接收信息 分页
                  systemNoticeHistorySelectedRows: [],
                  systemNoticeHistorySelectedRowKeys: []
                }
              });

            case 18:
            case "end":
              return _context15.stop();
          }
        }
      }, queryReceiver);
    }),
    // 新建 接收配置
    createReceiver: /*#__PURE__*/_regeneratorRuntime.mark(function createReceiver(_ref29, _ref30) {
      var payload, call, noticeId, records, res;
      return _regeneratorRuntime.wrap(function createReceiver$(_context16) {
        while (1) {
          switch (_context16.prev = _context16.next) {
            case 0:
              payload = _ref29.payload;
              call = _ref30.call;
              noticeId = payload.noticeId, records = payload.records;
              _context16.next = 5;
              return call(_createReceiver, noticeId, records);

            case 5:
              res = _context16.sent;
              return _context16.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context16.stop();
          }
        }
      }, createReceiver);
    }),
    // 发布 SystemNotice
    publishSystemNotice: /*#__PURE__*/_regeneratorRuntime.mark(function publishSystemNotice(_ref31, _ref32) {
      var payload, call, noticeId, records, res;
      return _regeneratorRuntime.wrap(function publishSystemNotice$(_context17) {
        while (1) {
          switch (_context17.prev = _context17.next) {
            case 0:
              payload = _ref31.payload;
              call = _ref32.call;
              noticeId = payload.noticeId, records = payload.records;
              _context17.next = 5;
              return call(_publishSystemNotice, noticeId, records);

            case 5:
              res = _context17.sent;
              return _context17.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context17.stop();
          }
        }
      }, publishSystemNotice);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref33) {
      var payload = _ref33.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};